/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package acceso.servlet;

import wf.dto.pfins_tbfrawor;
import wf.dto.pptraconfin;
import wf.dto.pptraconini;
import wf.dto.pptransafin;
import wf.dto.pptransaini;
import wf.dto.pptrapagfin;
import wf.dto.pptrapagini;
import wf.dto.ins_tbexcept;
import acceso.hibernate.AQuery;
import acceso.hibernate.AccesoHibernate;
import acceso.util.Escritor;
import java.util.Date;
import org.hibernate.*;

/**
 *
 * @author romulogalindo
 */
public class Transa_Engine {
    
    //public static pptransaini Transa_inicio(String ip_remoto, int Id_session) throws HibernateException{
    public static pptransaini Transa_inicio(StatelessSession HSESSION,String ip_remoto, int Id_session, Date fe_solini) throws HibernateException{
        pptransaini transaini;
        AQuery IHQUERY = new AQuery(HSESSION.getNamedQuery("frawor2.pptransaini"));
        IHQUERY.setString("ip_remoto", ip_remoto);
        IHQUERY.setInteger("id_session", Id_session);
        IHQUERY.setTimestamp("fe_solini", fe_solini);
        Escritor.escribe_transa(IHQUERY.getQuery());
        transaini = (pptransaini) IHQUERY.getMquery().uniqueResult();
        return transaini;
    }
    
    public static pptransafin Transa_fin(int id_transa, Date fe_solfin) throws HibernateException{
        StatelessSession HSESSION = null;
        Transaction HTRANSA = null;
        pptransafin transafin = null;
        try{
            HSESSION = AccesoHibernate.getSessionFactory().openStatelessSession();
            AQuery IHQUERY = new AQuery(HSESSION.getNamedQuery("frawor2.pptransafin"));
            IHQUERY.setInteger("p_id_transa", id_transa);
            IHQUERY.setTimestamp("p_fe_solfin", fe_solfin);
            Escritor.escribe_transa(IHQUERY.getQuery());
            transafin = (pptransafin) IHQUERY.getMquery().uniqueResult();
        }finally{
            closeTransa(HSESSION, HTRANSA);
        }
        return transafin;
    }
    
    public static pptraconini Tracon_inicio(StatelessSession HSESSION, int id_transa, int id_frawor, int co_conten) throws HibernateException{
        pptraconini traconini = null;
        AQuery IHQUERY = new AQuery(HSESSION.getNamedQuery("frawor2.pptraconini"));
        IHQUERY.setInteger("p_id_transa", id_transa);
        IHQUERY.setInteger("p_id_frawor", id_frawor);
        IHQUERY.setInteger("p_co_conten", co_conten);        
        Escritor.escribe_transa(IHQUERY.getQuery());
        traconini = (pptraconini) IHQUERY.getMquery().uniqueResult();
        return traconini;
    }
    
    //public static pptraconfin Tracon_fin(int id_transa) throws HibernateException{
    public static pptraconfin Tracon_fin(StatelessSession HSESSION, int id_transa) throws HibernateException{
        pptraconfin traconfin = null;
        AQuery IHQUERY = new AQuery(HSESSION.getNamedQuery("frawor2.pptraconfin"));
        IHQUERY.setInteger("p_id_transa", id_transa);      
        Escritor.escribe_transa(IHQUERY.getQuery());
        traconfin = (pptraconfin) IHQUERY.getMquery().uniqueResult();
        return traconfin;
    }
    
    //public static pptrapagini Trapag_inicio(int id_transa, int id_frawor, int co_pagina) throws HibernateException{
    public static pptrapagini Trapag_inicio(StatelessSession HSESSION, int id_transa, int id_frawor, int co_pagina) throws HibernateException{
        pptrapagini trapagini = null;
        AQuery IHQUERY = new AQuery(HSESSION.getNamedQuery("frawor2.pptrapagini"));
        IHQUERY.setInteger("p_id_transa", id_transa);
        IHQUERY.setInteger("p_id_frawor", id_frawor);
        IHQUERY.setInteger("p_co_pagina", co_pagina);
        Escritor.escribe_transa(IHQUERY.getQuery());
        trapagini = (pptrapagini) IHQUERY.getMquery().uniqueResult();
        return trapagini;
    }
    
    public static pptrapagfin Trapag_fin(StatelessSession HSESSION, int id_transa, int co_pagina) throws HibernateException{
        pptrapagfin trapagfin = null;
        AQuery IHQUERY = new AQuery(HSESSION.getNamedQuery("frawor2.pptrapagfin"));
        IHQUERY.setInteger("p_id_transa", id_transa);
        IHQUERY.setInteger("p_co_pagina", co_pagina);
        Escritor.escribe_transa(IHQUERY.getQuery());
        trapagfin = (pptrapagfin) IHQUERY.getMquery().uniqueResult();
        return trapagfin;
    }

    public static pfins_tbfrawor Transa_Idfrawor(StatelessSession HSESSION, int id_session, int id_frawor, int id_fraant, int co_conten) throws HibernateException {
        pfins_tbfrawor sefr = null;
        AQuery IHQUERY = new AQuery(HSESSION.getNamedQuery("frawor2.pfins_tbfrawor"));
        IHQUERY.setInteger("p_id_sesion", id_session);
        IHQUERY.setInteger("p_id_frawor", id_frawor);
        IHQUERY.setInteger("p_id_fraant", id_fraant);
        IHQUERY.setInteger("p_co_conten", co_conten);
        Escritor.escribe_frawor(IHQUERY.getQuery());
        sefr = (pfins_tbfrawor) IHQUERY.getMquery().uniqueResult();
        return sefr;
    }

    public static void Transa_Ins_tbexcept(int id_frawor, int co_pagina, String no_except) throws HibernateException{
        StatelessSession HSESSION = null;
        Transaction HTRANSA = null;
        ins_tbexcept except = null;
        try{
            HSESSION = AccesoHibernate.getSessionFactory().openStatelessSession();
            AQuery IHQUERY = new AQuery(HSESSION.getNamedQuery("frawor2.ins_tbexcept"));
            IHQUERY.setInteger("p_id_frawor", id_frawor);
            IHQUERY.setInteger("p_co_pagina", co_pagina);
            IHQUERY.setString("p_no_except", no_except);
            Escritor.escribe_transa(IHQUERY.getQuery());
            except= ( ins_tbexcept) IHQUERY.getMquery().uniqueResult();
        }finally{
            closeTransa(HSESSION, HTRANSA);
        }
    }
    
    public static void Transa_Ins_fe_pagina( int id_frawor ,long id_ransa, int co_pagina, long fe_visini, long fe_visfin) throws HibernateException{
        StatelessSession HSESSION = null;
        Transaction HTRANSA = null;
        //ins_tbexcept except = null;
        try{
            HSESSION = AccesoHibernate.getSessionFactory().openStatelessSession();
            //AQuery IHQUERY = new AQuery(HSESSION.getNamedQuery("frawor2.ins_tbexcept"));
            AQuery IHQUERY = new AQuery(HSESSION.createSQLQuery("update fratra.tptrapag set fe_visini=:p_fe_visini , fe_visfin =:p_fe_visfin where id_transa="+id_ransa+" and id_frawor="+id_frawor+" and co_pagina = "+co_pagina+";"));
            //IHQUERY.setInteger("p_co_pagina", co_pagina);
            IHQUERY.setTimestamp("p_fe_visini", new Date(fe_visini));
            IHQUERY.setTimestamp("p_fe_visfin", new Date(fe_visfin));
            Escritor.escribe_transa(IHQUERY.getQuery());
            int r = IHQUERY.getMquery().executeUpdate();
            if(r != 1) Escritor.escribe_errors("No se pudo actualizar las fechas del tiempo de visualización de la página: "+co_pagina+".");
            //except= ( ins_tbexcept) IHQUERY.getMquery().uniqueResult();
        }finally{
            closeTransa(HSESSION, HTRANSA);
        }
    }
    
    public static void closeTransa(StatelessSession HSESSION,Transaction HTRANSA) throws HibernateException{
        try{
            //System.out.println("================================================INI");
            //System.out.println("session:["+HSESSION+"], transa:["+HTRANSA+"]");
            if (HSESSION != null) {
                try{
                    HSESSION.close();
                    //System.out.println("session cerrada");
                }catch(Exception ep){
                    HSESSION = null;
                    //System.out.println("session ["+ep.getMessage()+"]>session null");
                }
            }
            //System.out.println("================================================FIN\n\n");
        }catch(Exception ep){
            Escritor.escribe_errors("Workflow no pudo cerrar la transacción: "+ep.getClass()+"\n"+ep.getMessage());
            //System.out.println("================================================FIN2\n\n");
        }
    }
    
}